<template>
	<view>
		<!-- 搜索栏 -->
		<view class="search-box">
			<uni-search-bar @input="search" radius="30" placeholder="搜索" cancelButton="none"/>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history" v-if="kw === ''">
			<!-- 标题 -->
			<view class="title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="cleanHistory"></uni-icons>
			</view>
			
			<!-- 内容 -->
			<view class="content">
				<uni-tag @click="goList(item)" type="primary" inverted="true" :text="item" v-for="item,i in reHistory" :key="i"></uni-tag>
			</view>
		</view>
		
		<!-- 搜索列表 -->
		<view class="search-list" v-else>
			<view @click="goDetail(item)" class="search-item" v-for="(item,index) in searchList" :key="index">
				<text>{{item.goods_name}}</text>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchList: [],
				timer: null,
				kw: '',
				history: JSON.parse(uni.getStorageSync('history') || '[]')
			}
		},
		computed: {
			reHistory() {
				return [...this.history].reverse()
			}
		},
		methods: {
			// 跳转到商品详情页面
			goDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods-detail/goods-detail?goods_id='+item.goods_id
				})
			},
			// 跳转到商品列表页面
			goList(item) {
				uni.navigateTo({
					url:'/subpkg/goods-list/goods-list?query='+item
				})
			},
			// 清空搜索历史
			cleanHistory() {
				// 清空 data 中保存的搜索历史
				this.history = []
				// 清空本地存储中记录的搜索历史
				uni.setStorageSync('history', '[]')
			},
			// 搜索历史
			saveHistory() {
				const set = new Set(this.history)
				set.delete(this.kw)
				set.add(this.kw)
				this.history = Array.from(set)
				// 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
				uni.setStorageSync('history', JSON.stringify(this.history))
			},
			// 搜索
			search(val) {
				this.kw = val
				// 清除上一次定时器
				clearTimeout(this.timer)
				// console.log(val)
				// 判断用户输入是否为空
				if(val.trim() === '') {
					this.searchList = []
					return
				}
				// 防抖
				this.timer = setTimeout(async () => {
					// 获取搜索的商品列表
					const {data: {message, meta}} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: val})
					if(meta.status !== 200) return uni.$showMsg()
					this.searchList = message
					// console.log(message)
					this.saveHistory()
				}, 500)
			}
		}
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
	left: 0;
}
.search-item {
	display: flex;
	justify-content: space-between;
	height: 40px;
	border-bottom: 1px solid #eee;
	align-items: center;
	padding: 0 6px;
	font-size: 12px;
	text {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
}
.uni-searchbar {
	background-color: #ea4350;
}
uni-tag {
	margin: 0 0 10px 6px;
	// display: block;
}
.history {
	.title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #eee;
		height: 40px;
		font-size: 12px;
		padding: 0 6px;
	}
	.content {
		display: flex;
		flex-wrap: wrap;
		padding-top: 6px;
	}
}
</style>
